package top.yokey.android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.App;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class ModifyGenderActivity extends BaseActivity {

    private AppCompatImageView backImageView;
    private AppCompatRadioButton manRadioButton;
    private AppCompatRadioButton womanRadioButton;
    private AppCompatTextView modifyTextView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_gender);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        manRadioButton = findViewById(R.id.manRadioButton);
        womanRadioButton = findViewById(R.id.womanRadioButton);
        modifyTextView = findViewById(R.id.modifyTextView);

        if (App.get().getMemberData().getMemberGender().isEmpty()) {
            manRadioButton.setChecked(true);
            womanRadioButton.setChecked(false);
        } else {
            if (App.get().getMemberData().getMemberGender().equals("1")) {
                manRadioButton.setChecked(true);
                womanRadioButton.setChecked(false);
            } else {
                manRadioButton.setChecked(false);
                womanRadioButton.setChecked(true);
            }
        }

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> finish());

        manRadioButton.setOnClickListener(view -> {
            manRadioButton.setChecked(true);
            womanRadioButton.setChecked(false);
        });

        womanRadioButton.setOnClickListener(view->{
            manRadioButton.setChecked(false);
            womanRadioButton.setChecked(true);
        });

        modifyTextView.setOnClickListener(view -> {
            String gender = manRadioButton.isChecked() ? "1" : "2";
            //防止多次点击
            modifyTextView.setEnabled(false);//禁用按钮
            modifyTextView.setText("正在修改...");//修改按钮文字
            //发送注册请求,注意IP地址改成自己本机的IP地址
            HttpHelp.get().ready("member", "modifyGender")
                    .add("gender", gender)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            App.get().getMemberData().setMemberGender(gender);
                            Toast.makeText(ModifyGenderActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(ModifyGenderActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);//启用按钮
                            modifyTextView.setText("修改性别");//修改按钮文字
                        }
                    });
        });

    }

}
